package com.stargate.bd.dao.factory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;

import com.stargate.bd.bean.Entree;
import com.stargate.bd.bean.Etat;
import com.stargate.bd.dao.DAO;

public class EntreeDAO extends DAO<Entree> {

	@Override
	public ArrayList<Entree> selectAll() throws SQLException {
		String sql =  "SELECT * FROM " + Entree.TABLE_NAME;
		ArrayList<Entree> entrees = new ArrayList<Entree>();
		Statement statement = connection.createStatement();
		ResultSet resultat = statement.executeQuery(sql);
		while (resultat.next()) {
			entrees.add(new Entree(resultat.getInt(Entree.NUM_ACCES),
						resultat.getString(Entree.CODE_BATIMENT),
						resultat.getString(Entree.CODE_POINT_ACCES),
						Etat.valueOf(resultat.getString(Entree.ETAT_ENTREE)),
						resultat.getDate(Entree.DATE_ENTREE)));
		}
		resultat.close();
		statement.close();
		return entrees;
	}

	@Override
	public Entree select(Entree bean) throws SQLException {
		ArrayList<Entree> entrees = selectAll();
		int index = entrees.indexOf(bean);
		return index == -1 ? null : entrees.get(index);
	}

	@Override
	public void update(Entree bean) throws SQLException {
		String sql = "UPDATE " + Entree.TABLE_NAME +
				" SET " + Entree.ETAT_ENTREE + "=?, " + Entree.DATE_ENTREE + "=? " +
				"WHERE " + Entree.NUM_ACCES + "=? " +
				"AND " + Entree.CODE_BATIMENT + "=? " +
				"AND " + Entree.CODE_POINT_ACCES + "=?";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getEtatEntree().getEtat(), Types.VARCHAR);
		statement.setObject(2, bean.getDateEntree(), Types.DATE);
		statement.setObject(3, bean.getNumAcces(), Types.INTEGER);
		statement.setObject(4, bean.getCodeBatiment(), Types.VARCHAR);
		statement.setObject(5, bean.getCodePointAcces(), Types.CHAR);
		statement.executeUpdate();
		statement.close();
	}

	@Override
	public void delete(Entree bean) throws SQLException {
		String sql = "DELETE FROM " + Entree.TABLE_NAME + 
				" WHERE " + Entree.NUM_ACCES + "=? " +
				"AND " + Entree.CODE_BATIMENT + "=? " +
				"AND " + Entree.CODE_POINT_ACCES + "=?";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getNumAcces(), Types.INTEGER);
		statement.setObject(2, bean.getCodeBatiment(), Types.VARCHAR);
		statement.setObject(3, bean.getCodePointAcces(), Types.CHAR);
		statement.executeUpdate();
		statement.close();
	}

	public void delete(int numAcces, String codeBatiment, String codePointAcces) throws SQLException {
		delete(new Entree(numAcces, codeBatiment, codePointAcces, null, null));
	}

	@Override
	public void insert(Entree bean) throws SQLException {
		String sql = "INSERT INTO " + Entree.TABLE_NAME + " VALUES (?,?,?,?,?)";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setObject(1, bean.getNumAcces(), Types.INTEGER);
		statement.setObject(2, bean.getCodeBatiment(), Types.VARCHAR);
		statement.setObject(3, bean.getCodePointAcces(), Types.CHAR);
		statement.setObject(4, bean.getEtatEntree().getEtat(), Types.VARCHAR);
		statement.setObject(5, bean.getDateEntree(), Types.DATE);
		statement.executeUpdate();
		statement.close();
	}

}
